package NC104_比较版本号;

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 比较版本号
     * @param version1 string字符串
     * @param version2 string字符串
     * @return int整型
     */
    public int compare (String version1, String version2) {
        // write code here
        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        segmentation(list1, version1);
        segmentation(list2, version2);
        int len1 = version1.length();
        int len2 = version2.length();

        int p1 = 0;
        int p2 = 0;

        for (int i = 0; i < Math.max(len1, len2); i++){
            p1 = i < len1 ? list1.get(i) : 0;
            p2 = i < len2 ? list2.get(i) : 0;
            if (p1 > p2){
                return 1;
            }else if (p1 < p2){
                return -1;
            }
        }
        return 0;
    }

    private void segmentation(ArrayList<Integer> list, String version) {
        int len = version.length();
        int num = 0;
        for (int i = 0; i < len; i++){
            if (version.charAt(i) == '.'){
                list.add(num);
                num = 0;
            }else {
                num = num * 10 + (version.charAt(i) - '0');
            }
        }
        list.add(num);
    }
}